package db

import (
	"fmt"

	_ "github.com/go-sql-driver/mysql"
	"github.com/linclaus/stock/pkg/model"
	"github.com/sirupsen/logrus"
	"github.com/xormplus/xorm"
)

var engine *xorm.Engine

var MYSQL_CON string

func InitDB() {
	e, err := xorm.NewEngine("mysql", MYSQL_CON)
	if err != nil {
		fmt.Printf("init db failed for reason: %s\n", err)
		logrus.Fatal(err)
		return
	}
	err = e.Sync2(new(model.User))
	if err != nil {
		fmt.Printf("init user table failed for reason: %s\n", err)
		logrus.Fatal(err)
		return
	}
	err = e.Sync2(new(model.UserStrategy))
	if err != nil {
		fmt.Printf("init userstrategy table failed for reason: %s\n", err)
		logrus.Fatal(err)
		return
	}
	err = e.Sync2(new(model.StockInfo))
	if err != nil {
		fmt.Printf("init stockInfo table failed for reason: %s\n", err)
		logrus.Fatal(err)
		return
	}
	e.ShowSQL(true)
	engine = e
}
